package com.bdqn.service.impl;



import com.bdqn.mapper.SysUserMapper;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import javax.annotation.Resource;
import java.util.List;

/**
 * SysUserServiceImpl
 *
 * @aurhor Administrator  whs
 * @since 2024/7/19
 */

@Transactional // 为该类的所有业务方法统一添加事务处理
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

    /**
     * 引入数据访问层
     */
    @Resource  //@ Resource
    private SysUserMapper sysUserMapper;

    @Transactional(propagation = Propagation.SUPPORTS)

    public SysUserMapper getSysUserMapper() {
        return sysUserMapper;
    }

    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    /**
     * 根据账号密码进行登录
     *
     * @param account
     * @param password
     * @return
     */
    @Override
    public SysUser login(String account, String password) {
        if (account == null || password == null){
            return null;
        }
        return sysUserMapper.queryByAccountPwd(account, password);
    }

    /**
     * 统计用户数量的方法
     *
     * @param queryRealName
     * @param queryRoleId
     * @return
     */
    @Override
    public int count(String queryRealName, Long queryRoleId) {
        return sysUserMapper.count(queryRealName, queryRoleId);
    }


    /**
     * 示例 6
     * 查询用户列表
     *
     * @return
     */
    @Override
    public List<SysUser> getUserList() {
        return sysUserMapper.getUserList();
    }

    /**
     * 分页查询用户列表
     *
     * @param realName
     * @param roleId
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public List<SysUser> selectPageList(String realName, Long roleId, Integer pageIndex, Integer pageSize) {
        return  sysUserMapper.selectPageList(realName, roleId, pageIndex, pageSize);
    }

    /**
     * 添加用户信息
     * @param sysUser
     * @return int 受影响的行数
     */
    @Override
    public boolean add(SysUser sysUser) {
        if (sysUser == null){
            return false;
        }
        return sysUserMapper.add(sysUser) == 1;
    }


    /**
     * 根据用户的真实姓名模糊匹配查询用户信息
     *
     * @param realName
     * @return
     */
    @Override
    public List<SysUser> getUsersByRealName(String realName) {
        return sysUserMapper.getUsersByRealName(realName);
    }

    /**
     * 根据用户id查询用户的详情信息
     *
     * @param id
     * @return
     */
    @Override
    public SysUser getUserById(Integer id) {
        return sysUserMapper.getUserById(id);
    }

    /**
     * 添加用户信息
     * @param sysUser
     * @return
     */
    @Override
    public boolean update(SysUser sysUser) {
        return sysUserMapper.update(sysUser);
    }

    /**
     * 判断账号是否存在
     *
     * @param account
     * @return
     */
    @Override
    public boolean exist(String account) {
        if (account == null){
            return false;
        }
        return sysUserMapper.exist(account)==1;
    }

    /**
     * 根据用户ID删除用户
     *
     * @param uploadPath
     * @param userId
     * @return -1表示不存在，1表示成功，0表示失败
     */
    @Override
    public int del(String uploadPath, Integer userId) {
        // 查找用户是否存在（不存在时返回-1）
        SysUser sysUser = this.getUserById(userId);
        if (sysUser == null) {
            return -1;
        }
        // 先删除用户上传的身份证图片数据
        String idPicPath = sysUser.getIdPicPath();
        if (idPicPath != null) {
            idPicPath = idPicPath.split("/")[3];
            File file = new File(uploadPath + "/" + idPicPath);
            boolean delete = file.delete();
            System.out.println("[" + file.getPath() + "]资源删除" + (delete ? "成功" : "失败"));
        }
        // 先删除用户上传的工作证图片数据
        String workPicPath = sysUser.getWorkPicPath();
        if (workPicPath != null) {
            workPicPath = workPicPath.split("/")[3];
            File file = new File(uploadPath + "/" + workPicPath);
            boolean delete = file.delete();
            System.out.println("[" + file.getPath() + "]资源删除" + (delete ? "成功" : "失败"));
        }
        return sysUserMapper.deleteUserById(userId);
    }


}
